package hong.jun.pet_home.controller.utilaudit;

import hong.jun.basic.util.AjaxResult;
import hong.jun.basic.util.FastDfsUtil;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/fastdfs")
public class FastdfsController {
    //fastdfs储存图片返回地址
    @PostMapping("/upload")
    public AjaxResult upload(@RequestPart(required = true, value = "file") MultipartFile file) {
        try {
            //获取原始文件名字
            String originalFilename = file.getOriginalFilename();
            // System.out.println("原始文件名字"+originalFilename);
            //获取文件的后缀名
            String extName = originalFilename.substring(originalFilename.lastIndexOf(".") + 1);
            // System.out.println("文件的后缀名"+extName);
            //文件上传
            String filePath = FastDfsUtil.upload(file.getBytes(), extName);
            //把返回的地址 ResultObj
            System.out.println("返回的地址" + filePath);
            return AjaxResult.me().setResultObj("http://124.222.92.214:8888" + filePath); //把上传后的路径
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(e.getMessage());
        }//http://124.222.92.214:8888/group1/M00/00/00/CgAMCmJiVj-AEwS4AAbSlRP1a2w389.jpg
    }

    //删除fastdfs图片
    @DeleteMapping("/deldfs")
    public AjaxResult deldfs(String path) {
        try {
            FastDfsUtil.delete("",path);
            return AjaxResult.me().setResultObj("删除成功");
        }catch (Exception e){
            e.printStackTrace();
            return new AjaxResult(e.getMessage());
        }
    }
}
